{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_data = [ 338., 333., 328., 207., 226., 25., 179., 60., 208., 606.]\n",
    "y_data = [ 640., 663., 619., 393., 428., 27., 193., 66., 226., 1591.]\n",
    "# ydata = b + w * xdata  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-200\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(-200, -100, 1) #bias\n",
    "y = np.arange(-5, 5, 0.1) #weight\n",
    "z = np.zeros((len(x), len(y)))\n",
    "#x, y = np.meshgrid(x ,y)\n",
    "\n",
    "for j in range(len(x)):\n",
    "    #print(x[j])\n",
    "    for i in range(len(y)):\n",
    "        b = x[j]\n",
    "        w = y[i]\n",
    "        z[j][i] = 0\n",
    "       \n",
    "        for n in range(len(x_data)):\n",
    "            #z[j][i] + (y_data[n] - b - w*x_data[n])**2\n",
    "            z[j][i] =  z[j][i] + (y_data[n] - b - w*x_data[n])**2\n",
    "        z[j][i] = z[j][i]/len(x_data)\n",
    "\n",
    "print (x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ydata = b + w*xdata\n",
    "b = -120 #initial b\n",
    "w = -4  # initial w\n",
    "\n",
    "lr = 0.0000001 # learning rate\n",
    "iteration = 100000\n",
    "\n",
    "# Store initial value for platting\n",
    "b_history = [b]\n",
    "w_history = [w]\n",
    "\n",
    "# Iterations\n",
    "for i in range(iteration):\n",
    "    \n",
    "    b_grad = 0.0\n",
    "    w_grad = 0.0\n",
    "    for n in range(len(x_data)):\n",
    "        b_grad = b_grad - 2.0*(y_data[n] - b - w*x_data[n])*1.0\n",
    "        w_grad = w_grad - 2.0*(y_data[n] - b - w*x_data[n])*x_data[n]\n",
    "        \n",
    "    #update parameters.\n",
    "    b = b - lr * b_grad\n",
    "    w = w - lr * w_grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZMAAAEQCAYAAAB1OJkXAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAAIABJREFUeJzt3XuYXHd93/H3d6/SrsaSbEk4kldZ2dgGX/BNNpaBemqDcai59IEQShORkOLStA08CYU65mL3edpwSwItScBNITFxSyGBkj4BAzaMi7FsdLHBsoWM1l68kgHLliWNZrXa269/zKy1Wq92Z+acM+f3+83n9Tz7aHcu5/z2eDzv/Z1zZsacc4iIiCTRkfcAREQkfIqJiIgkppiIiEhiiomIiCSmmIiISGKKiYiIJKaYiIhIYoqJiIgkppiIiEhiXXkPIKlVq1a5wcHBvIchIhKU7du3P+OcW53W8oKPyeDgINu2bct7GCIiQTGzn6W5vOBj8tRTE9xyyy/yHkbqSqXuzNdRLPZnvg4R8ccttyzJbNnBxyRWxeJE5usolSqZr6OVFEeR/CgmbawVwWqtgy2Z0bWSAimhUEwkKgqk3xTHeCkmIp6LK5BxxREUyBmKiYi0VFxxDO3Yow7Ai4h4KbY4NkuvgBcRkcSCn5mUy5OUSvubum+xmNqLP0VE2lrwMSkUjGKxs8l7H6BUmkp1PHlTIEUkD8HHJKnmQ+Sr+AIJiqSI79o+JjGKL5A0vSvTV4qjxEYxkSDEFkjFUWKjmIjkQHH0m+LYOMVERBJTHP3VqjAqJiIic8QUxxPDeHpm6/EyJmbWCWwD9jnnbsh7PCIioWpVGH19Bfx7gF15D0JEROrjXUzM7AzgnwF/lfdYRESkPt7FBPgU8H5gOu+BiIhIfbw6ZmJm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5Vu3wZ1bN1HqF6sOw/tOu2qF33m7VtsRP4+KzLN9YuGwI+Q+3Fu6F/LbQtZt3mFmpnc7Xj4wL4INXjig/N+lrTro+LNJ879Qp4ERFJLJjdXCIi4i/FREREElNMREQkMcVEREQSU0xERCQxxURERBJTTERSZmbnmJkzs1/LeywiraKYiKTv0tq/W3MdhUgLKSYi6bsMGHa19wUTaQeKiUj6LgO2mtmNZvaomVVqn/h46aL3FAmU3k5FJGVm9hzVt73fAfwl0E/17c57gXOcc5UF7i4SJK/egl4kdGZ2FtW3eX+A6jtcu9rlZaof6PYK4NsnX4JImLSbSyRdl9X+vdmdOO1/uPbv6haPR6QlFBORdF0KDDnnts+5/Fdq/+5FJEKKiUi6LmP+T6X7DeAQsKW1wxFpDR0zEUnXpcCTQAK4AAAAh0lEQVSYmXU55ybh+Y+JfTfwJ8658VxHJ5IRnc0lkhIzGwSeqH3dB3yB6udn3wLsA65RTCRW2s0lkp6Zg+83AEuB/wt8FPgq8FqFRGKmmYmIiCSmmYmIiCSmmIiISGKKiYiIJKaYiIhIYoqJiIgkppiIiEhiiomIiCSmmIiISGKKiYiIJPb/AWSGOhRSp+nKAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.6686842700023\n",
      "-188.16510178952453\n"
     ]
    }
   ],
   "source": [
    "# import plot\n",
    "# import matplotlib\n",
    "import matplotlib.pyplot as plt  # plt\n",
    "import numpy as np\n",
    "# 李宏毅原代码没有加载相关参数，以上为我自行加载的。\n",
    "\n",
    "x_data = [338, 333, 328, 207, 226, 25, 179, 60, 208, 606]\n",
    "y_data = [640, 633,619, 393, 428, 27, 193, 66, 226, 1591]\n",
    "\n",
    "x = np.arange(-200, -100, 1) # bias\n",
    "y = np.arange(-5, 5, 0.1) # weight\n",
    "Z = np.zeros((len(x), len(y)))\n",
    "X,Y = np.meshgrid(x, y)\n",
    "for i in range(len(x)):\n",
    "    for j in range(len(y)):\n",
    "        b = x[i]\n",
    "        w = y[j]\n",
    "        Z[j][i] = 0\n",
    "        for n in range(len(x_data)):\n",
    "            Z[j][i] = Z[j][i] + (y_data[n] - b - w*x_data[n])**2\n",
    "        Z[j][i] = Z[j][i]/len(x_data)\n",
    "\n",
    "# yadata = b + w*xdata\n",
    "b = -120 # intial b\n",
    "w = -4 # intial w\n",
    "lr = 0.000001 # learning rate\n",
    "iteration = 100000\n",
    "\n",
    "# store initial values for plotting\n",
    "b_history = [b]\n",
    "w_history = [w]\n",
    "\n",
    "# iterations\n",
    "for i in range(iteration):\n",
    "\n",
    "    b_grad = 0.0\n",
    "    w_grad = 0.0\n",
    "    for n in range(len(x_data)):\n",
    "        b_grad = b_grad + 2.0 * (b + w * x_data[n] - y_data[n]) * 1.0\n",
    "        w_grad = w_grad + 2.0 * (b + w * x_data[n] - y_data[n]) * x_data[n]\n",
    "\n",
    "    # update parameters\n",
    "    b = b - 0.0001*b_grad/len(x_data)\n",
    "    w = w - 0.000001*w_grad/len(x_data)\n",
    "\n",
    "    # store parameters for plotting\n",
    "    b_history.append(b)\n",
    "    w_history.append(w)\n",
    "\n",
    "# plot the figure\n",
    "plt.contourf(x, y, Z, 50, alpha=0.5, cmap=plt.get_cmap('jet'))\n",
    "plt.plot([-188.4], [2.67], 'x', ms=6, marker=6, color='orange')\n",
    "# 李宏毅课程原代码为markeredeweight=3,无法运行，改为了marker=3。\n",
    "# ms和marker分别代表指定点的长度和宽度。\n",
    "plt.plot(b_history, w_history, 'o-', ms=3, lw=1.5, color='black')\n",
    "plt.xlim(-200, -100)\n",
    "plt.ylim(-5, 5)\n",
    "plt.xlabel(r'$b$', fontsize=16)\n",
    "plt.ylabel(r'$w$', fontsize=16)\n",
    "plt.show()\n",
    "\n",
    "print(w)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[   2.66868427]\n",
      " [-188.16510179]]\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "shapes (2,1) and (100,100) not aligned: 1 (dim 1) != 100 (dim 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-e10726c27ccc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mW\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mW\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mYhead\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmatmul\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mW\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0mL\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mYhead\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mY\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0msum\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: shapes (2,1) and (100,100) not aligned: 1 (dim 1) != 100 (dim 0)"
     ]
    }
   ],
   "source": [
    "#判断的出结果的好坏\n",
    "W = np.zeros((2,1),)\n",
    "\n",
    "print(W)\n",
    "Yhead = np.matmul(W, np.transpose(X))\n",
    "L = Yhead - Y\n",
    "sum = 0\n",
    "row = 0\n",
    "for i in range(0,L.shape[1]):\n",
    "    print(str(Y[i]) + \" : \"+ str(Yhead[0, i]) + \" L: \" + str(L[0,i]))\n",
    "    sum += L[0,i]*L[0,i] \n",
    "    row += 1\n",
    "print(\"L:\" + str(sum/L.shape[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
